<template>
  <el-dialog
    title="修改个人信息"
    :visible.sync="visible"
    :append-to-body="true">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
        <el-form-item label="账号">
          <span>{{ userName }}</span>
        </el-form-item>
        <el-form-item label="真实姓名" prop="empName">
          <el-input v-model="dataForm.realname" placeholder="真实姓名"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="dataForm.empEmail" placeholder="邮箱"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="家庭地址" prop="mobile">
          <el-input v-model="dataForm.address" placeholder="家庭地址"></el-input>
        </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </span>
  </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            realname: '',
            empEmail: '',
            mobile: '',
            address: ''
          },
          dataRule: {
            realname: [
              { required: true, message: '真实姓名不能为空', trigger: 'blur' }
            ],
            empEmail: [
              { required: true, message: '电子邮件不能为空', trigger: 'blur' }
            ],
            mobile: [
              { required: true, message: '手机号不能为空', trigger: 'blur' }
            ],
            address: [
              { required: true, message: '地址不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      computed: {
        userName: {
          get () { return this.$store.state.user.name }
        }
      },
      methods: { // 初始化
        init () {
          this.visible = true
          this.$http({
            url: this.$http.adornUrl(`/gomoney-auth/sys/user/findDetail`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.username = data.detail.username
              this.dataForm.realname = data.detail.realname
              // this.dataForm.birthday = this.timestampToTime(data.detail.birthday)
              this.dataForm.empEmail = data.detail.empEmail
              this.dataForm.mobile = data.detail.mobile
              this.dataForm.address = data.detail.address
            }
          })
        },
        timestampToTime (cjsj) {
          var date = new Date(cjsj)
          var Y = date.getFullYear() + '-'
          var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
          var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
          return Y + M + D
        },
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/gomoney-auth/sys/user/updateDetail`),
                method: 'post',
                data: this.$http.adornData({
                  'username': this.dataForm.username,
                  'realname': this.dataForm.realname,
                  'empEmail': this.dataForm.empEmail,
                  'mobile': this.dataForm.mobile,
                  'address': this.dataForm.address
                })
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$router.replace({ name: 'home' })
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>

<style scoped>

</style>
